package com.zenithsun.common.basicdata.dao.impl;

import java.util.List;

import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import com.syzg.common.dao.impl.BaseDAOHibernate;
import com.zenithsun.common.basicdata.dao.BasicdataDAO;
import com.zenithsun.common.basicdata.model.Basicdata;

@Repository("basicdataDao")
public class BasicdataDAOImpl extends BaseDAOHibernate<Basicdata> implements BasicdataDAO {

	private static String SQL_GetTreeBasicDataByParentCode="SELECT C.* FROM BASICDATA C WHERE C.CODE=? UNION SELECT B.* FROM BASICDATA A LEFT JOIN BASICDATA B ON B.PARENT_ID=A.ID WHERE A.CODE=?";
	
	private static String SQL_GetBasicDataByParentCode="SELECT B.* FROM BASICDATA A LEFT JOIN BASICDATA B ON B.PARENT_ID=A.ID WHERE A.CODE=?";

	private static String SQL_UpdateBasicData="UPDATE BASICDATA A SET A.comment=?,A.remark=? WHERE A.id=?";

	@Override
	protected Class<Basicdata> getEntityClass() {
		return Basicdata.class;
	}

	@Override
	public List<Basicdata> getTreeNewsBasicData(String parentCode) throws Exception {
		SQLQuery sqlQuery=getSession().createSQLQuery(SQL_GetTreeBasicDataByParentCode);
		sqlQuery.setParameter(0, parentCode);
		sqlQuery.setParameter(1, parentCode);
		sqlQuery.addEntity(getEntityClass());
		return sqlQuery.list();
	}

	@Override
	public List<Basicdata> getNewsBasicData(String parentCode) throws Exception {
		SQLQuery sqlQuery=getSession().createSQLQuery(SQL_GetBasicDataByParentCode);
		sqlQuery.setParameter(0, parentCode);
		sqlQuery.addEntity(getEntityClass());
		return sqlQuery.list();
	}

	@Override
	public boolean updateApp(Basicdata basicdata) throws Exception {
		SQLQuery sqlQuery=getSession().createSQLQuery(SQL_UpdateBasicData);
		sqlQuery.setParameter(0, basicdata.getComment());
		sqlQuery.setParameter(1, basicdata.getRemark());
		sqlQuery.setParameter(2, basicdata.getId());
		return sqlQuery.executeUpdate()>0;
	}
}